//
//  LYVideoInfoManager.m
//  LYouSubscribeApp
//
//  Created by grx on 2018/12/17.
//  Copyright © 2018年 grx. All rights reserved.
//

#import "LYVideoInfoManager.h"

@implementation LYVideoInfoManager
static LYVideoInfoManager *info;

/** 购物车本地数据库管理 */
+ (LYVideoInfoManager *)share {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        info = [[LYVideoInfoManager alloc]init];
    });
    return info;
}
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        info = [super allocWithZone:zone];
    });
    return info;
}

- (id)copyWithZone:(NSZone *)zone {
    
    return info;
}

/** 收藏视频到本地数据库 */
-(void)addVideoToLocalData:(LYouVideoModel *)videoModel{
    NSArray *videoListData = [LYouVideoInfo MR_findAll];
    for (LYouVideoInfo *info in videoListData) {
        if ([info.videoId isEqualToString:videoModel.post_id]) {
            return;
        }
    }
    LYouVideoInfo *info = [LYouVideoInfo MR_createEntity];
    info.videoId = videoModel.post_id;
    info.videoUrl = videoModel.video_url;
    info.videoCover = videoModel.thumbnail_url;
    info.videoIsFavor = @"1";
    [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
//    [EasyShowTextView showText:@"收藏成功"];
}

/** 删除视频从本地数据库 */
-(void)deleteVideoToLocalData:(LYouVideoModel *)videoModel{
    NSPredicate *infoFilter = [NSPredicate predicateWithFormat:@"videoId CONTAINS %@",videoModel.post_id];
    NSArray *allVideoData = [LYouVideoInfo MR_findAllWithPredicate:infoFilter];
    for (LYouVideoInfo *info in allVideoData) {
        [info MR_deleteEntity];
    }
    [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
//    [EasyShowTextView showText:@"取消收藏成功"];
}

/** 查询本地所有数据 */
-(NSArray *)searchAllLocalVideoData{
    NSArray *videoListData = [LYouVideoInfo MR_findAll];
    return videoListData;
}

/** 删除本地所有数据 */
-(void)cancleAllLocalShopData{
    [LYouVideoInfo MR_truncateAll];
    [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
}

@end
